﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using System.Threading;
using System.Text;
using EPlanet.Web.UI.WebControls;
using DreamBuilder.UI.UserControls.DataAccess.Services;
using DreamBuilder.Core.Container;
using DreamBuilder.DictionaryAccess;

namespace DreamBuilder.UI.UserControls
{
    public class CheckBoxListReference : EditItemBase
    {
        public override event VoucherItemSelectedEventHandler VoucherItemSelected;
        CBLReference cblReference = null;
        VoucherEditItemInfo editItemInfo = null;
        FieldDicInfo fieldDicInfo = null;
        public  CheckBoxListReference(EditArgs editArgs)
        {
            if (editArgs.UserControl != null)
            {
                cblReference = (CBLReference)editArgs.UserControl.LoadControl("CBLReference.ascx");
            }
            editItemInfo = DictionaryAccessFacade.GetVoucherEditItemInfo(editArgs.ID, editArgs.ItemCode);
            fieldDicInfo = DictionaryAccessFacade.GetFieldDicInfo(editItemInfo.TableCode, editItemInfo.FieldCode);
            InstantiateIn(editArgs);
        }
         public override string Value
        {
            get
            {
                return this.cblReference.Value;
            }
        }
        public override Control Control
        {
            get
            {
                return cblReference;
            }
        }
          protected override void SetControlID()
        {
            FieldDicInfo fieldDicInfo = DictionaryAccessFacade.GetFieldDicInfo(editItemInfo.TableCode, editItemInfo.FieldCode);
            cblReference.ID = "chbl_" + editItemInfo.TableCode + "__" + editItemInfo.FieldCode;
            string sReferenceObj = fieldDicInfo.ReferenceObj;
            cblReference.TableCode = sReferenceObj;
        }

        protected override void SetControlValue(EditArgs editArgs)
        {

            if (fieldDicInfo.ReferenceObj != null && fieldDicInfo.ReferenceObj.Length > 0)
            {
                cblReference.TableCode = fieldDicInfo.ReferenceObj;
                string sResult = "";
                string columnName = editItemInfo.TableCode + "." + editItemInfo.FieldCode;
                if (editArgs.EditData.Rows.Count > 0 && editArgs.EditData.Rows[0][columnName] != System.DBNull.Value)
                {
                    sResult = editArgs.EditData.Rows[0][columnName].ToString();
                }
                if (sResult.Trim().Length > 0)
                {
                    cblReference.Value = sResult;
                }
                else
                {
                    if (!string.IsNullOrEmpty(editItemInfo.InitValue))
                    {
                        UCEditDAL ucEditManager = UContainer.Resolve<UCEditDAL>();
                        if (ucEditManager.IsReferenceTableHasValue(cblReference.TableCode, editItemInfo.InitValue))
                            cblReference.Value = editItemInfo.InitValue;
                    }
                }
                if (!string.IsNullOrEmpty(editItemInfo.RefRangeSql))
                {
                    cblReference.WhereSql = cblReference.WhereSql == "" ? editItemInfo.RefRangeSql : ("(" + editItemInfo.RefRangeSql + ") and (" + cblReference.WhereSql + ")");
                }
            }
        }
        protected override void SetControlAttributes(EditArgs editArgs)
        {
            cblReference.Enable = (!editItemInfo.ReadOnly);
            if (editItemInfo.AutoPostBack == true)
            {
                cblReference.RefSelected += new CBLSelectHandler(pCBLReference_RefSelected);
            }
            else
            {
                cblReference.AutoPostBack = false;
            }
            VoucherTotalInfo voucherTotalInfo = DictionaryAccessFacade.GetVoucherTotalInfo(editArgs.ID);
            cblReference.RepeatColumns = voucherTotalInfo.EditItemCols;

        }
        private void pCBLReference_RefSelected(object sender, EventArgs e)
        {
            if (VoucherItemSelected != null)
            {
                VoucherItemSelected(cblReference, e);
            }
        }
    }
}
